package cn.cmkj.auction.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jude.rollviewpager.adapter.StaticPagerAdapter;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.cmkj.auction.R;
import cn.cmkj.auction.data.http.ShuhuaImage;
import cn.cmkj.auction.ui.ImagePagerActivity;

/**
 * Created by cunguoyao on 2017/8/30.
 */

//适配器
public class ShuhuaImagePagerAdapter extends StaticPagerAdapter {

    private List<ShuhuaImage> imageUrls;
    private Context context;
    private ImageOptions imageOptions;

    public ShuhuaImagePagerAdapter(Context context, List<ShuhuaImage> imageUrls) {
        this.imageUrls = imageUrls;
        this.context = context;
        this.imageOptions = new ImageOptions.Builder().setFadeIn(true).setFailureDrawableId(R.drawable.empty_photo).build(); //淡入效果
    }

    @Override
    public View getView(ViewGroup container, final int position) {
        ImageView imageView = new ImageView(container.getContext());
        if(imageUrls != null && imageUrls.size() > 0) {
            ShuhuaImage image = imageUrls.get(position);
            x.image().bind(imageView, image.m_url, imageOptions);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    imageBrower(position, getUrls(imageUrls));
                }
            });
        }
        return imageView;
    }

    @Override
    public int getCount() {
        return imageUrls == null ? 0 : imageUrls.size();
    }

    private ArrayList<String> getUrls(List<ShuhuaImage> imageUrls) {
        ArrayList<String> list = new ArrayList<>();
        if(imageUrls != null && imageUrls.size() > 0) {
            for(ShuhuaImage image : imageUrls) {
                list.add(image.l_url);
            }
        }
        return list;
    }

    private void imageBrower(int position, ArrayList<String> urls) {
        Intent intent = new Intent(context, ImagePagerActivity.class);
        // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
        context.startActivity(intent);
    }
}
